package top.my.api;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.MultiMap;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonObject;
import io.vertx.example.util.Runner;
import io.vertx.ext.web.client.HttpResponse;
import io.vertx.ext.web.client.WebClient;
import top.my.Const;
import top.my.bean.User;
import top.my.utils.StrUtils;

/*
 * @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
 */
public class Client extends AbstractVerticle {

    // Convenience method so you can run it in your IDE
    public static void main(String[] args) {
        Runner.runExample(Client.class);
    }

    long time1 = 0L;

    @Override
    public void start() throws Exception {

        WebClient client = WebClient.create(vertx);

        MultiMap form = MultiMap.caseInsensitiveMultiMap();
        form.add("uid","6");
        form.add("uid","10");
        form.add("username", "test3");
        //form.add("password", StrUtils.md5("1234567"));
        //form.add("name", "我是管理员");
        //form.add("token", "60F45CC570CE7BF88C7740DB9A0F9224");
        //form.add("ct", "1639618837271");
        /*for (int i = 0; i < 1; i++) {
            long finalI = i;
            client.post(8080, "127.0.0.1", Const.API_LOGIN).sendForm(form, ar -> {
                if (ar.succeeded()) {
                    if (finalI == 0) {
                        time1 = System.currentTimeMillis();
                    }
                    HttpResponse<Buffer> response = ar.result();
                    if (finalI == 0) {
                        System.out.println("Got HTTP response with status " + response.statusCode() + ",body=" + response.bodyAsString());
                        System.out.println("时间:" + (System.currentTimeMillis() - time1));
                    }
                } else {
                    ar.cause().printStackTrace();
                }
            });

        }*/

        /*client.post(8080, "127.0.0.1", Const.API_REGISTER).sendForm(form, ar -> {
            if (ar.succeeded()) {
                HttpResponse<Buffer> response = ar.result();
                System.out.println("Got HTTP response with status " + response.statusCode() + ",body=" + response.bodyAsString());
            } else {
                ar.cause().printStackTrace();
            }
        });*/

        /*client.post(8080, "127.0.0.1", Const.API_UPDATE_INFO).sendForm(form, ar -> {
            if (ar.succeeded()) {
                HttpResponse<Buffer> response = ar.result();
                System.out.println("Got HTTP response with status " + response.statusCode() + ",body=" + response.bodyAsString());
            } else {
                ar.cause().printStackTrace();
            }
        });*/

        /*client.post(8080, "127.0.0.1", Const.API_UNREGISTER).sendForm(form, ar -> {
            if (ar.succeeded()) {
                HttpResponse<Buffer> response = ar.result();
                System.out.println("Got HTTP response with status " + response.statusCode() + ",body=" + response.bodyAsString());
            } else {
                ar.cause().printStackTrace();
            }
        });*/

        JsonObject user = new JsonObject();
        user.put("uid",6);
        client.post(8080, "127.0.0.1", Const.API_GET_INFO_BY_ID).sendForm(form, ar -> {
            if (ar.succeeded()) {
                HttpResponse<Buffer> response = ar.result();
                System.out.println("Got HTTP response with status " + response.statusCode() + ",body=" + response.bodyAsString());
            } else {
                ar.cause().printStackTrace();
            }
        });

    }
}
